
<template>
  <slot name="customUpload">
  <el-upload
    class="upload-demo" 
    action="#"
    :http-request="handleUpload"
    :on-success="handleSuccess"
    :on-error="handleError"
    :file-list="fileList"
    :limit="1"
    :before-upload="beforeAvatarUploadOther"
  >
  <el-button type="primary">请选择文件</el-button>
      <!-- <template #tip>
        <div class="el-upload__tip">jpg/png files with a size less than 500kb</div>
      </template> -->
  </el-upload>
</slot>
</template>

<script setup>
import { ref ,defineEmits} from 'vue'
import { useMessage, useMessageBox } from '@/hooks/message'

import {apiImportData} from '@/api/modules/EW.js'

const emits = defineEmits(['upload'])
const fileList = ref([])

// const beforeUpload = (file) => {
//   const isJPG = file.type === 'image/jpeg'
//   const isPNG = file.type === 'image/png'
//   const isLt500k = file.size / 1024 / 1024 < 0.5

//   if (!isJPG && !isPNG) {
//     this.$message.error('上传图片只能是 JPG/PNG 格式!')
//     return false
//   }
//   if (!isLt500k) {
//     this.$message.error('上传图片大小不能超过 500KB!')
//     return false
//   }
//   return true
// }

const handleUpload = async (params) => {
  console.log(params,'params')
  const formData = new FormData()
  formData.append('file', params.file)
  console.log(formData,'formData')
  // emits('upload',formData)
  
}

const handleSuccess = (response, file, fileList) => {
  console.log('文件上传成功', fileList,file.raw)
  emits('upload',file.raw)
}

const handleError = (error, file, fileList) => {
  console.log('文件上传失败', error)
}
//上传文件和上传附件之前的校验
const beforeAvatarUploadOther = (file) => {
				let str = file.name
				let index = str.lastIndexOf('.')
				str = str.substring(index + 1, str.length)
        console.log(str,'strstr')
				const isTypeTrue = /^(xlsx|xls|word|doc|docx|XLS|XLSX|DOC|WORD)$/.test(str);
				if (!isTypeTrue) {
          useMessage().warning('上传文件格式不对')
				}

				// const isLt40M = file.size / 1024 / 1024 < 40
				// if (!isLt40M) {
				// 	this.$message.error('上传文件大小不能超过 40MB')
				// 	file = null
				// 	this.fileList = []
				// 	// this.$refs.upload.clearFiles(); // 清除前端显示的文件列表
				// 	return false
				// } else {
				// 	if (file.status === 'ready') {
				// 		//this.uploadFile = file.raw;
				// 	}
				// }
				// this.fileList.length == 1
				return isTypeTrue
			}
</script>

<style>
.upload-demo {
  width: 300px;
}
</style>


